package com.xone.xapp;

import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import com.xone.xapp.HttpUtils.OnPostSuccessListener;

public abstract class AbstractPostDataTask extends AsyncTask<List<NameValuePair>, Integer, JSONObject> {
	protected Context context;
	public AbstractPostDataTask(Context context) {
		this.context = context;
	}
	@Override
	protected JSONObject doInBackground(List<NameValuePair>... params) {
		if (isCancelled()) {
			return null;
		}
		return HttpUtils.httpPost(Constants.APP_URL + getPostUrl(), params[0], 
				new OnPostSuccessListener<JSONObject>() {
					@Override
					public JSONObject onSuccess(int status, String result) {
						if (HttpUtils.SC_OK == status) {
							return JSONUtils.getJSONObjectFromString(result);
						} else {
							Toast.makeText(context, "Status Code:" + status, Toast.LENGTH_SHORT).show();
						}
						return null;
					}
				}, context);
	}
	@Override
	protected void onPostExecute(JSONObject result) {
		super.onPostExecute(result);
		postFinish(result);
	}
	
	public abstract String getPostUrl();
	
	public abstract void postFinish(JSONObject result);

}
